// dnSpy decompiler from Assembly-CSharp.dll class: ArpaPlusAds.InterstitialCrossAds
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

namespace ArpaPlusAds
{
	public class InterstitialCrossAds : MonoBehaviour
	{
		public static InterstitialCrossAds instance
		{
			get
			{
				return InterstitialCrossAds._instance;
			}
		}

		private void Awake()
		{
			if (InterstitialCrossAds._instance != null)
			{
				UnityEngine.Object.Destroy(base.gameObject);
			}
			else
			{
				InterstitialCrossAds._instance = this;
			}
			this.advPanel.SetActive(false);
			base.StartCoroutine(this.DownloadINI());
		}

		private IEnumerator DownloadINI()
		{
			this.UpdateLaunchCounters();
			WWW www = new WWW(this.hostUrl + "/" + this.iniSettingsFileName);
			yield return www;
			if (string.IsNullOrEmpty(www.error))
			{
				File.WriteAllText(Application.persistentDataPath + "/crossAdsImg.jpg", www.text);
				INIParser iniparser = new INIParser();
				iniparser.OpenFromString(www.text);
				this.settings.stop = iniparser.ReadValue("Settings", "stop", true);
				this.settings.runcount = iniparser.ReadValue("Settings", "runcount", 0);
				this.settings.repeat = iniparser.ReadValue("Settings", "runcount", 0);
				this.settings.pkgid = iniparser.ReadValue("Settings", "pkgid", string.Empty);
				string text = iniparser.ReadValue("Settings", "picture", string.Empty);
				iniparser.Close();
				if (!text.Equals(this.settings.picture))
				{
					this.settings.picture = text;
					base.StartCoroutine(this.DownloadAndCacheInterstitial());
				}
				else if (!File.Exists(Application.persistentDataPath + "/crossAdsImg.jpg"))
				{
					base.StartCoroutine(this.DownloadAndCacheInterstitial());
				}
				else
				{
					this.isLoaded = true;
				}
			}
			else
			{
				UnityEngine.Debug.Log("Error on ini download: " + www.error);
				yield return new WaitForSeconds(10f);
				base.StartCoroutine(this.DownloadINI());
			}
			www.Dispose();
			yield break;
		}

		private IEnumerator DownloadAndCacheInterstitial()
		{
			if (string.IsNullOrEmpty(this.settings.picture))
			{
				UnityEngine.Debug.Log("Interstitial picture url is empty");
			}
			else
			{
				WWW www = new WWW(this.hostUrl + "/" + this.settings.picture);
				yield return www;
				if (string.IsNullOrEmpty(www.error))
				{
					File.WriteAllBytes(Application.persistentDataPath + "/crossAdsImg.jpg", www.bytes);
					this.isLoaded = true;
				}
				else
				{
					UnityEngine.Debug.Log("Error on picture download: " + www.error);
					yield return new WaitForSeconds(10f);
					base.StartCoroutine(this.DownloadAndCacheInterstitial());
				}
			}
			yield break;
		}

		private void UpdateLaunchCounters()
		{
			if (PlayerPrefs.HasKey("LAUNCH_COUNTER"))
			{
				PlayerPrefs.SetInt("LAUNCH_COUNTER", PlayerPrefs.GetInt("LAUNCH_COUNTER") + 1);
			}
			else
			{
				PlayerPrefs.SetInt("LAUNCH_COUNTER", 1);
			}
			if (PlayerPrefs.HasKey("REPEAT_LAUNCH_COUNTER"))
			{
				PlayerPrefs.SetInt("REPEAT_LAUNCH_COUNTER", PlayerPrefs.GetInt("REPEAT_LAUNCH_COUNTER") + 1);
			}
			else
			{
				PlayerPrefs.SetInt("REPEAT_LAUNCH_COUNTER", 1);
			}
		}

		public void Show()
		{
			if (this.isLoaded && !this.isShowed && !this.settings.stop)
			{
				if (PlayerPrefs.HasKey("ALREADY_SHOWING"))
				{
					if (this.settings.repeat < PlayerPrefs.GetInt("REPEAT_LAUNCH_COUNTER"))
					{
						UnityEngine.Debug.Log("Showing cross add");
						PlayerPrefs.SetInt("REPEAT_LAUNCH_COUNTER", 0);
						this.ShowInterstitial();
					}
				}
				else if (this.settings.runcount < PlayerPrefs.GetInt("LAUNCH_COUNTER"))
				{
					UnityEngine.Debug.Log("Showing cross add");
					PlayerPrefs.SetInt("ALREADY_SHOWING", 1);
					this.ShowInterstitial();
				}
			}
		}

		private void ShowInterstitial()
		{
			this.isShowed = true;
			if (File.Exists(Application.persistentDataPath + "/crossAdsImg.jpg"))
			{
				File.ReadAllBytes(Application.persistentDataPath + "/crossAdsImg.jpg");
				Texture2D texture2D = new Texture2D(2, 2);
				texture2D.LoadImage(File.ReadAllBytes(Application.persistentDataPath + "/crossAdsImg.jpg"));
				this.advTargetImage.texture = texture2D;
				this.advTargetImage.SetNativeSize();
				this.advPanel.SetActive(true);
			}
		}

		public void Hide()
		{
			this.advPanel.SetActive(false);
		}

		public void OnAdvClick()
		{
			string url = string.Empty;
			if (Application.platform != RuntimePlatform.IPhonePlayer)
			{
				if (Application.platform == RuntimePlatform.Android)
				{
					url = "market://details?id=" + this.settings.pkgid;
				}
				else
				{
					url = "https://play.google.com/store/apps/details?id=" + this.settings.pkgid;
				}
			}
			Application.OpenURL(url);
		}

		private const string INI_FILENAME = "crossAds.ini";

		private const string INTERSTITIAL_IMG_FILENAME = "crossAdsImg.jpg";

		private const string LAUNCH_COUNTER_PLAYERPREF = "LAUNCH_COUNTER";

		private const string REPEAT_LAUNCH_COUNTER_PLAYERPREF = "REPEAT_LAUNCH_COUNTER";

		private const string ALREADY_SHOWING_PLAYERPREF = "ALREADY_SHOWING";

		private static InterstitialCrossAds _instance;

		[SerializeField]
		private GameObject advPanel;

		[SerializeField]
		private RawImage advTargetImage;

		[SerializeField]
		private string hostUrl;

		[SerializeField]
		private string iniSettingsFileName;

		private bool isLoaded;

		private bool isShowed;

		private AdsSettings settings;
	}
}
